/**商品数据管理 */
import { defineStore } from "pinia";

import {
    getGoodsList,
    getGoodsTotal,
    addGoods,
    delGoods,
    editGoods,
    getAllGoodsList
} from "../api/goodsApi"

import { ElMessage } from "element-plus";

// 注册store
const useGoodStore = defineStore("goods", {
    state: () => {
        return {
            // 起始页码
            page: 1,
            size: 3,
            total: 0,
            goodsList: [],
            allGoodsList: []
        }
    },
    getters: {

    },
    actions: {
        GET_ALLGOODSLIST(list) {
            this.allGoodsList = list
        },
        GET_GOODSLIST(list) {
            this.goodsList = list
        },
        CHANGE_PAGE(page) {
            this.page = page
        },
        CHANGE_SIZE(size) {
            this.size = size
        },
        CHANGE_TOTAL(total) {
            this.total = total
        },
        // 获取商品总数列表
        getAllGoodsAction() {
            getAllGoodsList().then((res) => {
                console.log('获取商品总-----数据', res)
                if (res.code === 200) {
                    this.GET_ALLGOODSLIST(res.list)
                }
            })
        },
        // 分页获取商品数据
        getGoodsListActions() {
            getGoodsList(this.page, this.size).then((res) => {
                console.log('分页获取商品数据', res)
                if (res.code === 200) {
                    this.GET_GOODSLIST(res.list)
                }
            })
        },
        // 商品总数量
        getGoodsTototalActions() {
            getGoodsTotal().then((res) => {
                console.log('获取商品总数', res)
                if (res.code === 200) {
                    this.CHANGE_TOTAL(res.list[0].total)
                }
            })
        },
        // 新增
        addGoodsAction(goodsForm) {
            // 上传图片需要将普通表单数据转换成formData对象
            const formData = new FormData()
            for (let key in goodsForm) {
                // 将表单数据添加到formData对象中
                formData.append(key, goodsForm[key])
            }
            addGoods(formData).then((res) => {
                if (res.code === 200) {
                    ElMessage({
                        type: "success",
                        message: "新增商品成功"
                    })
                    // 重新请求数据
                    this.getGoodsListActions()
                    this.getGoodsTototalActions()
                } else {
                    ElMessage({
                        type: "error",
                        message: res.msg
                    })
                }
            })
        },
        // 删除商品
        delGoodsActions(id) {
            delGoods(id).then((res) => {
                if (res.code === 200) {
                    ElMessage({
                        type: "success",
                        message: "删除商品成功"
                    })
                    // 重新请求数据
                    this.getGoodsListActions()
                    this.getGoodsTototalActions()
                } else {
                    ElMessage({
                        type: "error",
                        message: res.msg
                    })
                }
            })
        },
        // 编辑商品
        editGoodsActions(goodsForm) {
            // 删除多余的属性
            // delete goodsForm['children']
            // 上传图片需要将普通表单数据转换成formData对象
            const formData = new FormData()
            for (let key in goodsForm) {
                // 将表单数据添加到formData对象中
                formData.append(key, goodsForm[key])
            }
            editGoods(formData).then((res) => {
                // console.log('编辑的商品结果', res)
                // console.log('dispatch', dispatch)
                if (res.code === 200) {
                    ElMessage({
                        type: "success",
                        message: "商品编辑成功"
                    })
                    // 重新请求数据
                    this.getGoodsListActions()
                } else {
                    ElMessage({
                        type: "error",
                        message: res.msg
                    })
                }
            })
        },
    },
})
// 导出store
export default useGoodStore